package com.jjsc.product.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.jjsc.common.core.utils.StringUtils;
import com.jjsc.common.core.web.controller.BaseController;
import com.jjsc.common.core.web.domain.AjaxResult;
import com.jjsc.product.api.domain.Product;
import com.jjsc.product.api.domain.ProductSku;
import com.jjsc.product.service.ICategoryService;
import com.jjsc.product.service.IProductService;
import com.jjsc.product.service.IProductSkuService;
import com.jjsc.product.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: 钟城明
 * @CreateTime: 2025-10-12
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/channel")
@Tag(name = "聚合数据")
public class channelController extends BaseController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductSkuService productSkuService;
    @Autowired
    private ICategoryService categoryService;

    /**
     * 首页数据
     */
    @Operation(summary = "首页数据")
    @GetMapping("/index")
    public AjaxResult index() {
        IndexDataVo vo = new IndexDataVo();
        List<CategoryVo> categoryVoList = categoryService.getLevelOneCategory();
        vo.setCategoryList(categoryVoList);
        List<ProductSkuVo> productSkuVoList = productSkuService.getTopTenProductSkuList();
        vo.setProductList(productSkuVoList);
        return AjaxResult.success(vo);
    }

    @Operation(summary = "分类数据")
    @GetMapping("/category")
    public AjaxResult category() {
        List<CategoryVo> tree = categoryService.tree();
        return AjaxResult.success(tree);
    }

    @Operation(summary = "2级分类数据下的商品")
    @GetMapping("/category/{category2Id}/products")
    public AjaxResult getCategoryProducts(@PathVariable Long category2Id) {
        List<ProductSkuVo> productSkuVoList = productSkuService.getProductSkuListByCategory2Id(category2Id);
        return AjaxResult.success(productSkuVoList);
    }

    @Operation(summary = "商品详情")
    @GetMapping("/product/{skuId}")
    public AjaxResult getProduct(@PathVariable Long skuId) {
        ItemVo itemVo = new ItemVo();
        //封装商品sku
        ProductSku productSku = productSkuService.getSkuInfoById(skuId);
        itemVo.setProductSku(productSku);
        //封装商品sku价格
        itemVo.setSkuPriceVo(productSkuService.selectSkuPriceById(skuId));
        //封装商品轮播图
        Long productId = productSku.getProductId();
        Product product = productService.getById(productId);
        if (product != null && StringUtils.isNotBlank(product.getCoverImg())) {
            itemVo.setCoverImgList(product.getCoverImg().split(","));
        }
        //封装商品规格
        String specValue = product.getSpecValue();
        if (StringUtils.isNotBlank(specValue)) {
            JSONArray specValueList = JSON.parseArray(specValue);
            itemVo.setSpecValueList(specValueList);
        }
        //封装商品规格参数跟SkuId映射关系Map
        itemVo.setSpecValueMap(productSkuService.getSkuSpecValue(productId));
        return success(itemVo);
    }

}
